package day2.problem3;

import java.util.*;
import java.lang.*;

//数组中两个字符串的最小距离
//https://www.nowcoder.com/questionTerminal/2c6a0a8e1d20492f92941400036e0890

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String str1 = in.next();
        String str2 = in.next();
        int ret = Integer.MAX_VALUE, f1 = -1, f2 = -1;
        for(int i = 0; i<n; i++) {
            String str = in.next();
            if(str1.equals(str)) {
                f1 = i;
            }
            else if(str2.equals(str)) {
                f2 = i;
            }
            if(f1 != -1 && f2 != -1) {
                ret = Math.min(ret, Math.abs(f1-f2));
            }
        }
        if(f1 == -1 || f2 == -1) ret = -1;
        // System.out.println(f1 + " " + f2);
        System.out.println(ret);
    }
}